import React from "react"
import { Route, Redirect, Switch} from 'react-router-dom'
import { Suspense } from 'react'
function RouterView(props) {
    let { routes } = props
    return (<>
        <Suspense fallback="<h1>正在加载...</h1>">
            <Switch>
                {
                  
                    routes.map((item, index) => { 
                        let userinfo =JSON.parse(localStorage.getItem('logindata') || "{}") 
                        if (item.requireLogin && !userinfo.token) {
                              return <Route key={index} path={item.path}  exact={item.exact}>
                               <Redirect to="/login" />
                               </Route>
                        }
                            if (item.component) {
                                return <Route key={index} path={item.path} component={item.component} exact={item.exact}></Route>
                            }
                            else {
                                return <Route key={index} exact={item.exact} path={item.path}><Redirect to={item.redirect} /></Route>
                            }

                    })
                   
                }
                
            </Switch>
        </Suspense>
    </>)

}
export default RouterView